/**

   chunksrv.c
   ==========

   chunk server

**/



/**

   Includes
   --------

   - stdlib.h      : malloc, free
   - dc.h          : datacenter
   - chunksrv.h    : self header

**/

#include <stdlib.h>
#include "dc.h"
#include "chunksrv.h"



/**

   Function: struct chunksrv* chunksrv_create(char* ip, uint16_t port, uint64_t avail)
   -----------------------------------------------------------------------------------

   Create a chunk server

 **/


struct chunksrv* chunksrv_create(char* ip, uint16_t port, uint64_t avail)
{
  struct chunksrv* cs;
  uint8_t i=0;

  /* Simple sanity check */
  if ( ip == NULL )
    {
      return NULL;
    }

  /* Allocation */
  cs = (struct chunksrv*)malloc(sizeof(struct chunksrv));
  if (cs == NULL)
    {
      return NULL;
    }

  /* IP copy */
  while( (i<CHUNKSRV_IP_LEN-1) && (ip[i]) )
    {
      cs->ip[i] = ip[i];
      i++;
    }
  cs->ip[i] = 0;

  /* Fields */
  cs->port = port;
  cs->avail = avail;
  cs->dc = NULL;

  return cs;
}



/**

   Function: void chunksrv_delete(struct chunksrv* cs)
   ---------------------------------------------------

   Delete a chunk server

**/

void chunksrv_delete(struct chunksrv* cs)
{
  free(cs);
}


/**
   
   Function: int chunksrv_compare(struct chunksrv* cs1, struct chunksrv* cs2)
   --------------------------------------------------------------------------

   Compare chunk servers according to their available space

**/

int chunksrv_compare(struct chunksrv* cs1, struct chunksrv* cs2)
{
  if ( (cs1 != NULL) && (cs2 != NULL) )
    {
      return (cs2->avail - cs1->avail);
    }

  return 0;
}
